﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using nwrole.SuperModule.service.Models;

namespace nwrole.SuperModule.service.Controllers
{
    public class ModuleStatusController : ApiController
    {
        public IEnumerable<ModuleStatusDto> GetModuleStatuses()
        {
            SuperModule.CheckForModules();

            var moduleInfos = SuperModule.GetAllModuleInfos();

            return moduleInfos.Select(moduleInfo => new ModuleStatusDto(moduleInfo)).ToList();
        }

        [Route("api/ModuleStatus/{id}/StartModule")]
        public HttpResponseMessage StartModule(string id)
        {
            string moduleName = id;
            try
            {
                SuperModule.StartModule(moduleName);
            }
            catch
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            SuperModule.CheckForModules();
            var moduleInfo = SuperModule.GetModuleFromName(moduleName);
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new ModuleStatusDto(moduleInfo));
            return response;
        }

        [Route("api/ModuleStatus/{id}/StopModule")]
        public HttpResponseMessage StopModule(string id)
        {
            string moduleName = id;
            try
            {
                SuperModule.StopModule(moduleName);
            }
            catch
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            SuperModule.CheckForModules();
            var moduleInfo = SuperModule.GetModuleFromName(moduleName);
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new ModuleStatusDto(moduleInfo));
            return response;
        }
    }
}
